home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / itc / cc1.spur / explow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  15.0 KB  |  555 lines

  1. /* Subroutines for manipulating rtx's in semantically interesting ways.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "tree.h"
  25. #include "flags.h"
  26. #include "expr.h"
  27.  
  28. /* Return an rtx for the sum of X and the integer C.  */
  29.  
  30. rtx
  31. plus_constant (x, c)
  32.      register rtx x;
  33.      register int c;
  34. {
  35.   register RTX_CODE code = GET_CODE (x);
  36.   register enum machine_mode mode = GET_MODE (x);
  37.   int all_constant = 0;
  38.  
  39.   if (c == 0)
  40.     return x;
  41.  
  42.   if (code == CONST_INT)
  43.     return gen_rtx (CONST_INT, VOIDmode, (INTVAL (x) + c));
  44.  
  45.   /* If adding to something entirely constant, set a flag
  46.      so that we can add a CONST around the result.  */
  47.   if (code == CONST)
  48.     {
  49.       x = XEXP (x, 0);
  50.       all_constant = 1;
  51.     }
  52.   else if (code == SYMBOL_REF || code == LABEL_REF)
  53.     all_constant = 1;
  54.  
  55.   /* The interesting case is adding the integer to a sum.
  56.      Look for constant term in the sum and combine
  57.      with C.  For an integer constant term, we make a combined
  58.      integer.  For a constant term that is not an explicit integer,
  59.      we cannot really combine, but group them together anyway.  */
  60.  
  61.   if (GET_CODE (x) == PLUS)
  62.     {
  63.       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  64.     {
  65.       c += INTVAL (XEXP (x, 0));
  66.       x = XEXP (x, 1);
  67.     }
  68.       else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  69.     {
  70.       c += INTVAL (XEXP (x, 1));
  71.       x = XEXP (x, 0);
  72.     }
  73.       else if (CONSTANT_P (XEXP (x, 0)))
  74.     {
  75.       return gen_rtx (PLUS, mode,
  76.               plus_constant (XEXP (x, 0), c),
  77.               XEXP (x, 1));
  78.     }
  79.       else if (CONSTANT_P (XEXP (x, 1)))
  80.     {
  81.       return gen_rtx (PLUS, mode,
  82.               XEXP (x, 0),
  83.               plus_constant (XEXP (x, 1), c));
  84.     }
  85. #ifdef OLD_INDEXING
  86.       /* Detect adding a constant to an indexed address
  87.      of the form (PLUS (MULT (REG) (CONST)) regs-and-constants).
  88.      Keep the (MULT ...) at the top level of addition so that
  89.      the result is still suitable for indexing and constants
  90.      are combined.  */
  91.       else if (GET_CODE (XEXP (x, 0)) == MULT)
  92.     {
  93.       return gen_rtx (PLUS, mode, XEXP (x, 0),
  94.               plus_constant (XEXP (x, 1), c));
  95.     }
  96.       else if (GET_CODE (XEXP (x, 1)) == MULT)
  97.     {
  98.       return gen_rtx (PLUS, mode, plus_constant (XEXP (x, 0), c),
  99.               XEXP (x, 1));
  100.     }
  101. #endif
  102.     }
  103.   if (c != 0)
  104.     x = gen_rtx (PLUS, mode, x, gen_rtx (CONST_INT, VOIDmode, c));
  105.  
  106.   /* BEGIN PATCH HERE */
  107.   if (GET_CODE (x) == SYMBOL_REF)
  108.     return x;
  109.   /* END PATCH HERE */
  110.   if (all_constant)
  111.     return gen_rtx (CONST, mode, x);
  112.   else
  113.     return x;
  114. }
  115.  
  116. /* If X is a sum, return a new sum like X but lacking any constant terms.
  117.    Add all the removed constant terms into *CONSTPTR.
  118.    X itself is not altered.  The result != X if and only if
  119.    it is not isomorphic to X.  */
  120.  
  121. rtx
  122. eliminate_constant_term (x, constptr)
  123.      rtx x;
  124.      int *constptr;
  125. {
  126.   int c;
  127.   register rtx x0, x1;
  128.  
  129.   if (GET_CODE (x) != PLUS)
  130.     return x;
  131.  
  132.   /* First handle constants appearing at this level explicitly.  */
  133.   if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  134.     {
  135.       *constptr += INTVAL (XEXP (x, 0));
  136.       return eliminate_constant_term (XEXP (x, 1), constptr);
  137.     }
  138.  
  139.   if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  140.     {
  141.       *constptr += INTVAL (XEXP (x, 1));
  142.       return eliminate_constant_term (XEXP (x, 0), constptr);
  143.     }
  144.  
  145.   c = 0;
  146.   x0 = eliminate_constant_term (XEXP (x, 0), &c);
  147.   x1 = eliminate_constant_term (XEXP (x, 1), &c);
  148.   if (x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
  149.     {
  150.       *constptr += c;
  151.       return gen_rtx (PLUS, GET_MODE (x), x0, x1);
  152.     }
  153.   return x;
  154. }
  155.  
  156. /* Return an rtx for the size in bytes of the value of EXP.  */
  157.  
  158. rtx
  159. expr_size (exp)
  160.      tree exp;
  161. {
  162.   return expand_expr (size_in_bytes (TREE_TYPE (exp)), 0, SImode, 0);
  163. }
  164.  
  165. /* Not yet really written since C does not need it.  */
  166.  
  167. rtx
  168. lookup_static_chain ()
  169. {
  170.   abort ();
  171. }
  172.  
  173. /* Return a copy of X in which all memory references
  174.    and all constants that involve symbol refs
  175.    have been replaced with new temporary registers.
  176.    Also emit code to load the memory locations and constants
  177.    into those registers.
  178.  
  179.    If X contains no such constants or memory references,
  180.    X itself (not a copy) is returned.
  181.  
  182.    X may contain no arithmetic except addition, subtraction and multiplication.
  183.    Values returned by expand_expr with 1 for sum_ok fit this constraint.  */
  184.  
  185. static rtx
  186. break_out_memory_refs (x)
  187.      register rtx x;
  188. {
  189.   if (GET_CODE (x) == MEM || GET_CODE (x) == CONST
  190.       || GET_CODE (x) == SYMBOL_REF)
  191.     {
  192.       register rtx temp = force_reg (Pmode, x);
  193.       mark_reg_pointer (temp);
  194.       x = temp;
  195.     }
  196.   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  197.        || GET_CODE (x) == MULT)
  198.     {
  199.       register rtx op0 = break_out_memory_refs (XEXP (x, 0));
  200.       register rtx op1 = break_out_memory_refs (XEXP (x, 1));
  201.       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  202.     x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  203.     }
  204.   return x;
  205. }
  206.  
  207. /* Given a memory address or facsimile X, construct a new address,
  208.    currently equivalent, that is stable: future stores won't change it.
  209.  
  210.    X must be composed of constants, register and memory references
  211.    combined with addition, subtraction and multiplication:
  212.    in other words, just what you can get from expand_expr if sum_ok is 1.
  213.  
  214.    Works by making copies of all regs and memory locations used
  215.    by X and combining them the same way X does.
  216.    You could also stabilize the reference to this address
  217.    by copying the address to a register with copy_to_reg;
  218.    but then you wouldn't get indexed addressing in the reference.  */
  219.  
  220. rtx
  221. copy_all_regs (x)
  222.      register rtx x;
  223. {
  224.   if (GET_CODE (x) == REG)
  225.     {
  226.       if (REGNO (x) != FRAME_POINTER_REGNUM)
  227.     x = copy_to_reg (x);
  228.     }
  229.   else if (GET_CODE (x) == MEM)
  230.     x = copy_to_reg (x);
  231.   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  232.        || GET_CODE (x) == MULT)
  233.     {
  234.       register rtx op0 = copy_all_regs (XEXP (x, 0));
  235.       register rtx op1 = copy_all_regs (XEXP (x, 1));
  236.       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  237.     x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  238.     }
  239.   return x;
  240. }
  241.  
  242. /* Return something equivalent to X but valid as a memory address
  243.    for something of mode MODE.  When X is not itself valid, this
  244.    works by copying X or subexpressions of it into registers.  */
  245.  
  246. rtx
  247. memory_address (mode, x)
  248.      enum machine_mode mode;
  249.      register rtx x;
  250. {
  251.   register rtx tem, oldx;
  252.  
  253.   /* By passing constant addresses thru registers
  254.      we get a chance to cse them.  */
  255.   if (! cse_not_expected && CONSTANT_P (x))
  256.     return force_reg (Pmode, x);
  257.  
  258.   /* Accept a QUEUED that refers to a REG
  259.      even though that isn't a valid address.
  260.      On attempting to put this in an insn we will call protect_from_queue
  261.      which will turn it into a REG, which is valid.  */
  262.   if (GET_CODE (x) == QUEUED
  263.       && GET_CODE (QUEUED_VAR (x)) == REG)
  264.     return x;
  265.  
  266.   /* We get better cse by rejecting indirect addressing at this stage.
  267.      Let the combiner create indirect addresses where appropriate.
  268.      For now, generate the code so that the subexpressions useful to share
  269.      are visible.  But not if cse won't be done!  */
  270.   oldx = x;
  271.   if (! cse_not_expected && GET_CODE (x) != REG)
  272.     x = break_out_memory_refs (x);
  273.  
  274.   /* At this point, any valid address is accepted.  */
  275.   GO_IF_LEGITIMATE_ADDRESS (mode, x, win);
  276.  
  277.   /* If it was valid before but breaking out memory refs invalidated it,
  278.      use it the old way.  */
  279.   if (memory_address_p (mode, oldx))
  280.     goto win2;
  281.  
  282.   /* Perform machine-dependent transformations on X
  283.      in certain cases.  This is not necessary since the code
  284.      below can handle all possible cases, but machine-dependent
  285.      transformations can make better code.  */
  286.   LEGITIMIZE_ADDRESS (x, oldx, mode, win);
  287.  
  288.   /* PLUS and MULT can appear in special ways
  289.      as the result of attempts to make an address usable for indexing.
  290.      Usually they are dealt with by calling force_operand, below.
  291.      But a sum containing constant terms is special
  292.      if removing them makes the sum a valid address:
  293.      then we generate that address in a register
  294.      and index off of it.  We do this because it often makes
  295.      shorter code, and because the addresses thus generated
  296.      in registers often become common subexpressions.  */
  297.   if (GET_CODE (x) == PLUS)
  298.     {
  299.       int constant_term = 0;
  300.       rtx y = eliminate_constant_term (x, &constant_term);
  301.       if (constant_term == 0
  302.       || ! memory_address_p (mode, y))
  303.     return force_operand (x, 0);
  304.  
  305.       y = plus_constant (copy_to_reg (y), constant_term);
  306.       if (! memory_address_p (mode, y))
  307.     return force_operand (x, 0);
  308.       return y;
  309.     }
  310.   if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
  311.     return force_operand (x, 0);
  312.  
  313.   /* Last resort: copy the value to a register, since
  314.      the register is a valid address.  */
  315.   return force_reg (Pmode, x);
  316.  
  317.  win2:
  318.   x = oldx;
  319.  win:
  320.   if (flag_force_addr && optimize && GET_CODE (x) != REG
  321.       /* Don't copy an addr via a reg if it is one of our stack slots.
  322.      If we did, it would cause invalid REG_EQUIV notes for parms.  */
  323.       && ! (GET_CODE (x) == PLUS
  324.         && (XEXP (x, 0) == frame_pointer_rtx
  325.         || XEXP (x, 0) == arg_pointer_rtx)))
  326.     return force_reg (Pmode, x);
  327.   return x;
  328. }
  329.  
  330. /* Like `memory_address' but pretend `flag_force_addr' is 0.  */
  331.  
  332. rtx
  333. memory_address_noforce (mode, x)
  334.      enum machine_mode mode;
  335.      rtx x;
  336. {
  337.   int ambient_force_addr = flag_force_addr;
  338.   rtx val;
  339.  
  340.   flag_force_addr = 0;
  341.   val = memory_address (mode, x);
  342.   flag_force_addr = ambient_force_addr;
  343.   return val;
  344. }
  345.  
  346. /* Return a modified copy of X with its memory address copied
  347.    into a temporary register to protect it from side effects.
  348.    If X is not a MEM, it is returned unchanged (and not copied).
  349.    Perhaps even if it is a MEM, if there is no need to change it.  */
  350.  
  351. rtx
  352. stabilize (x)
  353.      rtx x;
  354. {
  355.   register rtx addr;
  356.   if (GET_CODE (x) != MEM)
  357.     return x;
  358.   addr = XEXP (x, 0);
  359.   if (rtx_unstable_p (addr))
  360.     {
  361.       rtx temp = copy_all_regs (addr);
  362.       rtx mem;
  363.       if (GET_CODE (temp) != REG)
  364.     temp = copy_to_reg (temp);
  365.       mem = gen_rtx (MEM, GET_MODE (x), temp);
  366.       /* Mark returned memref with in_struct
  367.      if it's in an array or structure. */
  368.       if (GET_CODE (addr) == PLUS || x->in_struct)
  369.     mem->in_struct = 1;
  370.       return mem;
  371.     }
  372.   return x;
  373. }
  374.  
  375. /* Copy the value or contents of X to a new temp reg and return that reg.  */
  376.  
  377. rtx
  378. copy_to_reg (x)
  379.      rtx x;
  380. {
  381.   register rtx temp = gen_reg_rtx (GET_MODE (x));
  382.   emit_move_insn (temp, x);
  383.   return temp;
  384. }
  385.  
  386. /* Like copy_to_reg but always give the new register mode Pmode
  387.    in case X is a constant.  */
  388.  
  389. rtx
  390. copy_addr_to_reg (x)
  391.      rtx x;
  392. {
  393.   register rtx temp = gen_reg_rtx (Pmode);
  394.   emit_move_insn (temp, x);
  395.   return temp;
  396. }
  397.  
  398. /* Like copy_to_reg but always give the new register mode MODE
  399.    in case X is a constant.  */
  400.  
  401. rtx
  402. copy_to_mode_reg (mode, x)
  403.      enum machine_mode mode;
  404.      rtx x;
  405. {
  406.   register rtx temp = gen_reg_rtx (mode);
  407.   if (GET_MODE (x) != mode && GET_MODE (x) != VOIDmode)
  408.     abort ();
  409.   emit_move_insn (temp, x);
  410.   return temp;
  411. }
  412.  
  413. /* Load X into a register if it is not already one.
  414.    Use mode MODE for the register.
  415.    X should be valid for mode MODE, but it may be a constant which
  416.    is valid for all integer modes; that's why caller must specify MODE.
  417.  
  418.    The caller must not alter the value in the register we return,
  419.    since we mark it as a "constant" register.  */
  420.  
  421. rtx
  422. force_reg (mode, x)
  423.      enum machine_mode mode;
  424.      rtx x;
  425. {
  426.   register rtx temp, insn;
  427.  
  428.   if (GET_CODE (x) == REG)
  429.     return x;
  430.   temp = gen_reg_rtx (mode);
  431.   insn = emit_move_insn (temp, x);
  432.   /* Let optimizers know that TEMP's value never changes
  433.      and that X can be substituted for it.  */
  434.   if (CONSTANT_P (x))
  435.     REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUIV, x, 0);
  436.   return temp;
  437. }
  438.  
  439. /* If X is a memory ref, copy its contents to a new temp reg and return
  440.    that reg.  Otherwise, return X.  */
  441.  
  442. rtx
  443. force_not_mem (x)
  444.      rtx x;
  445. {
  446.   register rtx temp;
  447.   if (GET_CODE (x) != MEM)
  448.     return x;
  449.   temp = gen_reg_rtx (GET_MODE (x));
  450.   emit_move_insn (temp, x);
  451.   return temp;
  452. }
  453.  
  454. /* Copy X to TARGET (if it's nonzero and a reg)
  455.    or to a new temp reg and return that reg.  */
  456.  
  457. rtx
  458. copy_to_suggested_reg (x, target)
  459.      rtx x, target;
  460. {
  461.   register rtx temp;
  462.   if (target && GET_CODE (target) == REG)
  463.     temp = target;
  464.   else
  465.     temp = gen_reg_rtx (GET_MODE (x));
  466.   emit_move_insn (temp, x);
  467.   return temp;
  468. }
  469.  
  470. /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
  471.    This pops when ADJUST is positive.  ADJUST need not be constant.  */
  472.  
  473. void
  474. adjust_stack (adjust)
  475.      rtx adjust;
  476. {
  477.   adjust = protect_from_queue (adjust, 0);
  478.  
  479. #ifdef STACK_GROWS_DOWNWARD
  480.   emit_insn (gen_add2_insn (stack_pointer_rtx, adjust));
  481. #else
  482.   emit_insn (gen_sub2_insn (stack_pointer_rtx, adjust));
  483. #endif
  484. }
  485.  
  486. /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
  487.    This pushes when ADJUST is positive.  ADJUST need not be constant.  */
  488.  
  489. void
  490. anti_adjust_stack (adjust)
  491.      rtx adjust;
  492. {
  493.   adjust = protect_from_queue (adjust, 0);
  494.  
  495. #ifdef STACK_GROWS_DOWNWARD
  496.   emit_insn (gen_sub2_insn (stack_pointer_rtx, adjust));
  497. #else
  498.   emit_insn (gen_add2_insn (stack_pointer_rtx, adjust));
  499. #endif
  500. }
  501.  
  502. /* Round the size of a block to be pushed up to the boundary required
  503.    by this machine.  SIZE is the desired size, which need not be constant.  */
  504.  
  505. rtx
  506. round_push (size)
  507.      rtx size;
  508. {
  509. #ifdef STACK_BOUNDARY
  510.   int align = STACK_BOUNDARY / BITS_PER_UNIT;
  511.   if (align == 1)
  512.     ;
  513.   if (GET_CODE (size) == CONST_INT)
  514.     {
  515.       int new = (INTVAL (size) + align - 1) / align * align;
  516.       if (INTVAL (size) != new)
  517.     size = gen_rtx (CONST_INT, VOIDmode, new);
  518.     }
  519.   else
  520.     {
  521.       size = expand_divmod (0, CEIL_DIV_EXPR, Pmode, size,
  522.                 gen_rtx (CONST_INT, VOIDmode, align),
  523.                 0, 1);
  524.       size = expand_mult (Pmode, size,
  525.               gen_rtx (CONST_INT, VOIDmode, align),
  526.               0, 1);
  527.     }
  528. #endif /* STACK_BOUNDARY */
  529.   return size;
  530. }
  531.  
  532. /* Return an rtx representing the register or memory location
  533.    in which a scalar value of data type VALTYPE
  534.    was returned by a function call to function FUNC.
  535.    FUNC is a FUNCTION_DECL node if the precise function is known,
  536.    otherwise 0.  */
  537.  
  538. rtx
  539. hard_function_value (valtype, func)
  540.      tree valtype;
  541.      tree func;
  542. {
  543.   return FUNCTION_VALUE (valtype, func);
  544. }
  545.  
  546. /* Return an rtx representing the register or memory location
  547.    in which a scalar value of mode MODE was returned by a library call.  */
  548.  
  549. rtx
  550. hard_libcall_value (mode)
  551.      enum machine_mode mode;
  552. {
  553.   return LIBCALL_VALUE (mode);
  554. }
  555.